package com.guo.takeoutframe;



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends FragmentActivity implements View.OnClickListener {

    @BindView(R.id.main_fragment_container)
    FrameLayout mainFragmentContainer;
    @BindView(R.id.main_bottome_switcher_container)
    LinearLayout mainBottomeSwitcherContainer;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    private ArrayList<Fragment> fragmentList;
    private FrameLayout indexFramelayout;
    private FrameLayout framelayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initFragment();
        setLisener();
        onClick(mainBottomeSwitcherContainer.getChildAt(0));
    }
    /*初始化fragment*/
    private void initFragment() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new OrderFragment());
        fragmentList.add(new MyFragment());
        fragmentList.add(new MoreFragment());
    }

    /*给底部栏的选项卡设置点击事件*/
    private void setLisener() {
        int childCount = mainBottomeSwitcherContainer.getChildCount();
        for(int i=0;i<childCount;i++)
        {
            indexFramelayout = (FrameLayout) mainBottomeSwitcherContainer.getChildAt(i);
            indexFramelayout.setOnClickListener(this);
        }
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {

        //1.选中的是哪一个
        int index = mainBottomeSwitcherContainer.indexOfChild(v);
        //2.选中之后改变颜色
        changeUi(index);
        //3.切换fragment
        changeFragment(index);

    }

    /*地步栏选项卡，选中之后颜色改变*/
    private void changeUi(int index) {
        int childCount = mainBottomeSwitcherContainer.getChildCount();
        for(int i=0;i<childCount;i++)
        {
            framelayout = (FrameLayout) mainBottomeSwitcherContainer.getChildAt(i);
            if (i == index){
                setEnable(framelayout,false);
            }else{
                setEnable(framelayout,true);
            }
        }}
    /*切换fragment*/
    private void changeFragment(int index) {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.main_fragment_container,fragmentList.get(index)).commit();
    }
        /**
         * @param view  设置是否可用的控件,并包含其孩子控件
         * @param isEnable  是否可用  是否可用已经和颜色绑定
         */
        private void setEnable(View view,boolean isEnable) {
            view.setEnabled(isEnable);
            //如果view是一个容器,即是viewGroup
            if (view instanceof ViewGroup){
                int childCount = ((ViewGroup) view).getChildCount();
                for (int i=0;i<childCount;i++){
                    setEnable(((ViewGroup) view).getChildAt(i),isEnable);
                }
            }
        }




}
